/*
 * Copyright 2007 Agile Plugins Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.agileplugins.monkey;

import com.intellij.codeInspection.*;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiModifier;
import com.intellij.psi.PsiModifierList;
import com.intellij.util.IncorrectOperationException;

public class RemovePrivateFieldInspection extends LocalInspectionTool {

    public String getGroupDisplayName() {
        return "PurpleMonkeyDishwasher";
    }

    public String getDisplayName() {
        return "Remove Private Field";
    }

    public String getShortName() {
        return "RemovePrivateField";
    }

    public ProblemDescriptor[] checkField(PsiField field, InspectionManager manager, boolean isOnTheFly) {
        if (checkIsTest(field.getContainingFile()) && field.getModifierList().hasModifierProperty(PsiModifier.PRIVATE)) {
            return new ProblemDescriptor[]{manager.createProblemDescriptor(field.getModifierList(), "The private modifier is no longer required",
                    new RemovePrivateFieldQuickFix(), ProblemHighlightType.LIKE_DEPRECATED)};
        }
        return null;
    }

    private boolean checkIsTest(PsiFile containingFile) {
        return containingFile.getName().endsWith("Test.java");
//
//        ProjectRootManager rootManager = ProjectRootManager
//                .getInstance(containingFile.getProject());
//        ProjectFileIndex fileIndex = rootManager.getFileIndex();
//
//        return fileIndex.isInTestSourceContent(containingFile.getVirtualFile());

    }

    private class RemovePrivateFieldQuickFix implements LocalQuickFix {

        public String getName() {
            return "Remove private modifier";
        }

        public void applyFix(Project project, ProblemDescriptor problemdescriptor) {
            PsiModifierList aClass = (PsiModifierList) problemdescriptor.getPsiElement();
            try {
                aClass.setModifierProperty(PsiModifier.PRIVATE, false);
            } catch (IncorrectOperationException e) {
                throw new RuntimeException(e);
            }
        }

        public String getFamilyName() {
            return "Remove private modifier";
        }
    }
}
